package com.guldan.jaina.common.filter;

import com.alibaba.dubbo.rpc.RpcContext;
import com.guldan.jaina.dto.users.UsersDTO;
import com.guldan.jaina.utils.UserUtils;
import net.sf.json.JSONObject;

import java.util.Map;

/**
 * Created by Silas.
 * Date: 2016/12/10
 * Time: 15:49
 */
public class DubboContextRecorder {

    public DubboContextRecorder() {
    }

    public static Map transferAttFrom() {
        Map attachments = RpcContext.getContext().getAttachments();
        return attachments;
    }

    public static UsersDTO initUserDTOAttFrom() {

        Map attachments = RpcContext.getContext().getAttachments();

        if (attachments != null && !attachments.isEmpty()) {
            if (attachments.containsKey("guldan-user")) {
                String userJson = (String) attachments.get("guldan-user");
                UsersDTO usersDTO = (UsersDTO) JSONObject.toBean(JSONObject.fromObject(userJson),
                        UsersDTO.class);

                UserUtils.setUser(usersDTO);
                return usersDTO;
            }
        }
        return null;
    }

}
